<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.File" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page import="org.springframework.util.FileCopyUtils" %>
<%@ page import="net.java_school.commons.WebContants" %>
<%
	//request.setCharacterEncoding("UTF-8");//필터가 이 작업을 대신 해준다.
	String filename = request.getParameter("filename");

	File file = new File(WebContants.BASE_PATH + filename);
	
	String filetype = filename.substring(filename.indexOf(".") + 1, filename.length());

	if (filetype.trim().equalsIgnoreCase("txt")) {
		response.setContentType("text/plain");
	} else {
		response.setContentType("application/octet-stream");
	}

	response.setContentLength((int) file.length());

	boolean ie = request.getHeader("User-Agent").indexOf("MSIE") != -1;
	if (ie) {
		filename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", " ");
	} else {
		filename = new String(filename.getBytes("UTF-8"), "8859_1");
	}
	
	response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
	
	OutputStream outputStream = response.getOutputStream();
	FileInputStream fis = null;

	try {
		fis = new FileInputStream(file);
		FileCopyUtils.copy(fis, outputStream);
	} finally {
		if (fis != null) {
			try {
				fis.close();
			} catch (IOException e) {}
		}
	}
	
	out.flush();
%>
